<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:24}" :lg="{span:6}">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col ::sm="{span:24}" :lg="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsnj" @change="zsndChange" disabled>
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:8}" style="padding-top: 29px;">
            <a-button type="primary" @click="handleCreate" v-hasPermission="'bsyxzsjh:add'">添加</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
			:pagination="pagination"
			@change="handleTableChange"
    >
      <template slot="action" slot-scope="text,record">
        <a @click="handleEdit(record.id,zbzt)" v-if="zbzt">修改</a>
        <a @click="handleEdit(record.id,zbzt)" v-if="!zbzt">查看</a>
		<a v-hasPermission="'bszsjh:delete'" @click="handleDelete(record.id)">删除</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <departments-add ref="departmentsAdd" @close="handleCreateClose" @success="handleCreateSuccess" :departmentsAddVisiable="departmentsAddVisiable"></departments-add>
    <!-- 编辑 -->
    <departments-edit ref="departmentsEdit" @close="handleEditClose" @success="handleEditSuccess" :info="departmentsEditData" :status="departStatus" :departmentsEditVisiable="departmentsEditVisiable"></departments-edit>
  </a-card>
</template>

<script>
// import moment from 'moment'
import DepartmentsAdd from './departmentsAdd'
import DepartmentsEdit from './departmentsEdit'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126']
export default {
  name: 'Departments',
  components: {
    STable,
    DepartmentsAdd,
    DepartmentsEdit
  },
  data () {
    return {
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        zsnj: '',
				zsyxm:''
      },
      zbzt:true,
      postParams: {},
      // 表头
      columns: [
        {
          title: '院系编码',
          dataIndex: 'zsyxm'
        },
        {
          title: '院系',
          dataIndex: 'dwmc'
        },
        {
          title: '总计划',
          dataIndex: 'zszjhrs'
        },
        {
          title: '学术学位计划',
          dataIndex: 'fzyxwjhrs'
        },
        {
          title: '专业学位计划',
          dataIndex: 'zyxwjhrs'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],

      // custom table alert & rowSelection
      options: {
        alert: { show: false }
      },
      departmentsAddVisiable: false,
      departmentsEditVisiable: false,
      selectMenu: {
        yx: [] // 院系
      },
      departStatus:"",
      departmentsEditData: {
        hzfs: '',
        zjh: '',
        fzyxwjh: '',
        zyxwjh: ''
      },
			pagination:{
				current:1,
        showSizeChanger: true,
        defaultPageSize: 30,
        pageSizeOptions: ['30','60'],
        // 改变pageSize后触发
        onShowSizeChange: (current, pageSize) => this.pagination.pageSize = pageSize,
			}
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.zsyxm=res[0].data[0].dwh;
					this.$api.base.majorAll({lsdwh: this.queryParam.zsyxm}).then(res => {
       				 this.$set(this.selectMenu, 'zy', res.data)
					  })
					  this.$api.base.getSchoolYear().then(res=>{
					this.queryParam.zsnj=res.data.doctoralAdmissionYear;
					this.loadData({
					pageNum:1,
					pageSize:30
					})
     				})
      })
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'zsyxm', code)
      // this.$api.base.majorAll({lsdwh: code}).then(res => {
      //   this.$set(this.selectMenu, 'zy', res.data)
      // })
    },
    // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.doctorRecruit.collegeEnList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
      })
      this.$api.doctorRecruit.collegeAble({
        zsyxm : this.queryParam.zsyxm,
        zsnj :this.queryParam.zsnj
      }).then(res =>{
        this.zbzt =res.data
      })
    },
		// 分页
		handleTableChange (pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
    // 选择招生年度
    zsndChange (code) {
			 this.$set(this.queryParam, 'zsnj', code)
    },
    // 添加
    handleCreate () {
      this.departmentsAddVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.departmentsAddVisiable = false
    },
    // 添加成功
    handleCreateSuccess () {
      this.departmentsAddVisiable = false
      this.$message.success('添加成功')
			this.loadData({
			  pageSize: 10,
			  pageNum: 1
			})
    },
    // 修改
    handleEdit (key,status) {
			console.log(key)
			this.$api.doctorRecruit.collegeEnId(key).then(res=>{
				// console.log(res)
				 this.departmentsEditVisiable = true
				if(res.data){
					this.departmentsEditData = res.data
          this.departStatus = status
				}
			})
      // const dataSource = [...this.data]
      // const target = dataSource.filter(item => item.key === key)[0]
      // this.departmentsEditData = {
      //   hzfs: '',
      //   zjh: target.zjh,
      //   fzyxwjh: target.fzyxwjh,
      //   zyxwjh: target.zyxwjh
      // }
      // console.log(this.departmentsEditData)

    },
    // 关闭添加弹层
    handleEditClose () {
      this.departmentsEditVisiable = false
    },
    // 添加成功
    handleEditSuccess () {
      this.departmentsEditVisiable = false
			this.loadData({
        pageSize: 10,
        pageNum: this.pagination.current
      })
      // this.$message.success('添加成功')
    },
	  // 删除
    handleDelete (key) {
      let _this=this;
      this.$confirm({
         title: '确定删除?',
         onOk() {
		 console.log("id"+key);
          _this.$api.doctorRecruit.collegeEnDelete(key).then(res=>{
        if(res.status==1){
          _this.$message.success(res.msg)
          _this.loadData({
            pageSize:10,
            pageNum:_this.pagination.current
          })
        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
    // 导出
    exportExcel () {
      console.log('导出')
      this.$export('admissions/bsyxzsjh/export',this.queryParam)
    }
  }
}
</script>
